import { defineConfig, globalIgnores } from 'eslint/config'
import globals from 'globals'
import js from '@eslint/js'
import pluginVue from 'eslint-plugin-vue'
import skipFormatting from '@vue/eslint-config-prettier/skip-formatting'
import fs from 'fs'

// 尝试导入自动生成的 ESLint 配置
let autoImportGlobals = {}
try {
  if (fs.existsSync('./.eslintrc-auto-import.json')) {
    const autoImportConfig = JSON.parse(fs.readFileSync('./.eslintrc-auto-import.json', 'utf8'))
    autoImportGlobals = autoImportConfig.globals || {}
  }
} catch (error) {
  console.error('读取自动导入 ESLint 配置失败:', error)
}

export default defineConfig([
  {
    name: 'app/files-to-lint',
    files: ['**/*.{js,mjs,jsx,vue}'],
  },

  globalIgnores(['**/dist/**', '**/dist-ssr/**', '**/coverage/**']),

  {
    languageOptions: {
      globals: {
        ...globals.browser,
        ...autoImportGlobals, // 使用自动生成的全局变量
      },
    },
  },

  js.configs.recommended,
  ...pluginVue.configs['flat/essential'],
  skipFormatting,
])
